

%%

cMP = mean(cs12.MP);
cNA = mean(cs12.NA);
cPVEM = mean(cs12.PVEM(1:N0));
cPRI = mean(cs12.PRI(1:N0));
cCM = mean(cs12.CM(N0+1:end));
cPAN = mean(cs12.PAN);


%% MP

cc = cMP - 2 * std(cs12.MP) + (cMP + 2 * std(cs12.MP)) * (0:100) / 100;
cc = cc(cc <= - B0(end-3) / (2 * B0(end-2)));

sMP = zeros(length(cc),3);
for i = 1:length(cc)
    XC = [X0,[repmat(cc(i),N,1);C(N+1:end)],[repmat(cc(i),N,1);C(N+1:end)].^2];
    [~,ss] = Shares([B0;xi0],zeros(size(S)),XC,KD,A,[N0,N1,N2],nu,wnu);
    ss = ss(1:N);
    sMP(i,1) = prctile(ss,5);
    sMP(i,2) = mean(ss);
    sMP(i,3) = prctile(ss,95);
end
sMP = 100 * sMP;

plot(10*cc,sMP(:,2),'LineWidth',2,'Color',[0.2,0.2,0.2])
hold on
plot(10*cc,sMP(:,1),'LineStyle','--','LineWidth',1,'Color',[0.5,0.5,0.5])
hold on
plot(10*cc,sMP(:,3),'LineStyle','--','LineWidth',1,'Color',[0.5,0.5,0.5])
ax = gca;
ax.YLabel.String = 'Vote Share (\% Registered Voters)';
ax.YLabel.FontSize = 14;
ax.YLabel.Interpreter = 'latex';
ax.YLim = [0,57];
ax.XLabel.String = 'Campaign Spending (Thousands of USD)';
ax.XLabel.FontSize = 14;
ax.XLabel.Interpreter = 'latex';
ax.XLim = 10 * [min(cc),max(cc)];
saveas(gcf,'../figures/figA4a.png')
hold off


%% NA

cc = cNA - 2 * std(cs12.NA) + (cNA + 2 * std(cs12.NA)) * (0:100) / 100;
cc = cc(cc <= - B0(end-3) / (2 * B0(end-2)));

sNA = zeros(length(cc),3);
for i = 1:length(cc)
    XC = [X0,[C(1:N);repmat(cc(i),N,1);C(2*N+1:end)],[C(1:N);repmat(cc(i),N,1);C(2*N+1:end)].^2];
    [~,ss] = Shares([B0;xi0],zeros(size(S)),XC,KD,A,[N0,N1,N2],nu,wnu);
    ss = ss(N+(1:N));
    sNA(i,1) = prctile(ss,5);
    sNA(i,2) = mean(ss);
    sNA(i,3) = prctile(ss,95);
end
sNA = 100 * sNA;

plot(10*cc,sNA(:,2),'LineWidth',2,'Color',[0.2,0.2,0.2])
hold on
plot(10*cc,sNA(:,1),'LineStyle','--','LineWidth',1,'Color',[0.5,0.5,0.5])
hold on
plot(10*cc,sNA(:,3),'LineStyle','--','LineWidth',1,'Color',[0.5,0.5,0.5])
ax = gca;
ax.YLabel.String = 'Vote Share (\% Registered Voters)';
ax.YLabel.FontSize = 14;
ax.YLabel.Interpreter = 'latex';
ax.XLabel.String = 'Campaign Spending (Thousands of USD)';
ax.XLabel.FontSize = 14;
ax.XLabel.Interpreter = 'latex';
ax.XLim = 10 * [min(cc),max(cc)];
saveas(gcf,'../figures/figA4b.png')
hold off


%% PVEM

cc = cPVEM - 2 * std(cs12.PVEM(1:N0)) + (cPVEM + 2 * std(cs12.PVEM(1:N0))) * (0:100) / 100;
cc =cc(cc <= - B0(end-3) / (2 * B0(end-2)));

sPVEM = zeros(length(cc),3);
for i = 1:length(cc)
    XC = [X0,[C(1:2*N);repmat(cc(i),N0,1);C(2*N+N0+1:end)],[C(1:2*N);repmat(cc(i),N0,1);C(2*N+N0+1:end)].^2];
    [~,ss] = Shares([B0;xi0],zeros(size(S)),XC,KD,A,[N0,N1,N2],nu,wnu);
    ss = ss(2*N+(1:N0));
    sPVEM(i,1) = prctile(ss,5);
    sPVEM(i,2) = mean(ss);
    sPVEM(i,3) = prctile(ss,95);
end
sPVEM = 100 * sPVEM;

plot(10*cc,sPVEM(:,2),'LineWidth',2,'Color',[0.2,0.2,0.2])
hold on
plot(10*cc,sPVEM(:,1),'LineStyle','--','LineWidth',1,'Color',[0.5,0.5,0.5])
hold on
plot(10*cc,sPVEM(:,3),'LineStyle','--','LineWidth',1,'Color',[0.5,0.5,0.5])
ax = gca;
ax.YLabel.String = 'Vote Share (\% Registered Voters)';
ax.YLabel.FontSize = 14;
ax.YLabel.Interpreter = 'latex';
ax.XLabel.String = 'Campaign Spending (Thousands of USD)';
ax.XLabel.FontSize = 14;
ax.XLabel.Interpreter = 'latex';
ax.XLim = 10 * [min(cc),max(cc)];
saveas(gcf,'../figures/figA4c.png')
hold off


%% PRI

cc = cPRI - 2 * std(cs12.PRI(1:N0)) + (cPRI + 2 * std(cs12.PRI(1:N0))) * (0:100) / 100;
cc = cc(cc <= - B0(end-3) / (2 * B0(end-2)));

sPRI = zeros(length(cc),3);
for i = 1:length(cc)
    XC = [X0,[C(1:2*N+N0+N2);repmat(cc(i),N0,1);C(2*(N+N0)+N2+1:end)],[C(1:2*N+N0+N2);repmat(cc(i),N0,1);C(2*(N+N0)+N2+1:end)].^2];
    [~,ss] = Shares([B0;xi0],zeros(size(S)),XC,KD,A,[N0,N1,N2],nu,wnu);
    ss = ss(2*N+N0+N2+(1:N0));
    sPRI(i,1) = prctile(ss,5);
    sPRI(i,2) = mean(ss);
    sPRI(i,3) = prctile(ss,95);
end
sPRI = 100 * sPRI;

plot(10*cc,sPRI(:,2),'LineWidth',2,'Color',[0.2,0.2,0.2])
hold on
plot(10*cc,sPRI(:,1),'LineStyle','--','LineWidth',1,'Color',[0.5,0.5,0.5])
hold on
plot(10*cc,sPRI(:,3),'LineStyle','--','LineWidth',1,'Color',[0.5,0.5,0.5])
ax = gca;
ax.YLabel.String = 'Vote Share (\% Registered Voters)';
ax.YLabel.FontSize = 14;
ax.YLabel.Interpreter = 'latex';
ax.XLabel.String = 'Campaign Spending (Thousands of USD)';
ax.XLabel.FontSize = 14;
ax.XLabel.Interpreter = 'latex';
ax.XLim = 10 * [min(cc),max(cc)];
saveas(gcf,'../figures/figA4d.png')
hold off


%% PRI + PVEM

cc = cCM - 2 * std(cs12.CM(N0+1:end)) + (cCM + 2 * std(cs12.CM(N0+1:end))) * (0:100) / 100;
cc = cc(cc <= - B0(end-3) / (2 * B0(end-2)));

sCM = zeros(length(cc),3);
for i = 1:length(cc)
    XC = [X0,[C(1:2*N+N0);repmat(cc(i),N2,1);C(2*N+N0+N2+(1:N0));repmat(cc(i),N1,1);C(3*N+N0+1:end)],...
        [C(1:2*N+N0);repmat(cc(i),N2,1);C(2*N+N0+N2+(1:N0));repmat(cc(i),N1,1);C(3*N+N0+1:end)].^2];
    [~,ss] = Shares([B0;xi0],zeros(size(S)),XC,KD,A,[N0,N1,N2],nu,wnu);
    ss = ss([2*N+N0+(1:N2),2*(N+N0)+N2+(1:N1)]);
    sCM(i,1) = prctile(ss,5);
    sCM(i,2) = mean(ss);
    sCM(i,3) = prctile(ss,95);
end
sCM = 100 * sCM;

plot(10*cc,sCM(:,2),'LineWidth',2,'Color',[0.2,0.2,0.2])
hold on
plot(10*cc,sCM(:,1),'LineStyle','--','LineWidth',1,'Color',[0.5,0.5,0.5])
hold on
plot(10*cc,sCM(:,3),'LineStyle','--','LineWidth',1,'Color',[0.5,0.5,0.5])
ax = gca;
ax.YLabel.String = 'Vote Share (\% Registered Voters)';
ax.YLabel.FontSize = 14;
ax.YLabel.Interpreter = 'latex';
ax.YLim = [0,54];
ax.XLabel.String = 'Campaign Spending (Thousands of USD)';
ax.XLabel.FontSize = 14;
ax.XLabel.Interpreter = 'latex';
ax.XLim = 10 * [min(cc),max(cc)];
saveas(gcf,'../figures/figA4e.png')
hold off


%% PAN

cc = cPAN - 2 * std(cs12.PAN) + (cPAN + 2 * std(cs12.PAN)) * (0:100) / 100;
cc = cc(cc <= - B0(end-3) / (2 * B0(end-2)));

sPAN = zeros(length(cc),3);
for i = 1:length(cc)
    XC = [X0,[C(1:3*N+N0);repmat(cc(i),N,1)],[C(1:3*N+N0);repmat(cc(i),N,1)].^2];
    [~,ss] = Shares([B0;xi0],zeros(size(S)),XC,KD,A,[N0,N1,N2],nu,wnu);
    ss = ss(3*N+N0+1:end);
    sPAN(i,1) = prctile(ss,5);
    sPAN(i,2) = mean(ss);
    sPAN(i,3) = prctile(ss,95);
end
sPAN = 100 * sPAN;

plot(10*cc,sPAN(:,2),'LineWidth',2,'Color',[0.2,0.2,0.2])
hold on
plot(10*cc,sPAN(:,1),'LineStyle','--','LineWidth',1,'Color',[0.5,0.5,0.5])
hold on
plot(10*cc,sPAN(:,3),'LineStyle','--','LineWidth',1,'Color',[0.5,0.5,0.5])
ax = gca;
ax.YLabel.String = 'Vote Share (\% Registered Voters)';
ax.YLabel.FontSize = 14;
ax.YLabel.Interpreter = 'latex';
ax.YLim = [0,54];
ax.XLabel.String = 'Campaign Spending (Thousands of USD)';
ax.XLabel.FontSize = 14;
ax.XLabel.Interpreter = 'latex';
ax.XLim = 10 * [min(cc),max(cc)];
saveas(gcf,'../figures/figA4f.png')
hold off



